<?php

class MemberAction extends BaseAction
{
    public function index()
    {
        $Member = D("Member");

        import("ORG.Util.Page");
        if ($_POST['keyword']) {
            $kmap = $_POST['keyword'];
            $map['username'] = array('like', '%' . $kmap . '%');
        } elseif ($_GET['keyword']) {
            $kmap = $_GET['keyword'];
            $map['username'] = array('like', '%' . $kmap . '%');
        }
        $count = $Member->where($map)->count();
        $Page = new Page($count, 10);
        $Page->parameter .= "keyword=" . urlencode($kmap) . "&";
        $show = $Page->show();
        $user = $Member->where($map)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('pages', $show);
        $this->assign("user", $user);
        $this->assign("title", '用户');
        $this->display("Public:member");
    }

    public function add()
    {
        $this->display("Public:member");
    }

    public function doadd()
    {
        $Member = D("Member");
        if ($Member->Create()) {
            $Member->password = md5($_POST['password']);
            if ($Member->add()) {
                $this->assign("jumpUrl", "__URL__");
                $this->success("添加成功！");
            } else {
                $this->error("添加失败！");
            }
        } else {
            $this->error($Member->getError());
        }
    }

    public function edit()
    {
        if ($_GET['id']) {

            $userInfo = D("Member")->getById($_GET['id']);
            $this->assign($userInfo);
            $this->display("Public:member");
        } else {
            $this->assign("jumpUrl", "__URL__");
            $this->error("数据不存在！");
        }
    }

    public function doedit()
    {
        $data = $_POST;
        if (!empty($data['newpwd'])) {
            $data['password'] = md5($data['newpwd']);
            unset($data['newpwd']);
        }
        fb($data);
        if (D("Member")->save($data)) {
            $this->success("修改成功！");
        } else {
            $this->error("修改失败！");
        }
    }

    public function batch()
    {
        $this->_batch();
    }

}

?>